<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入角色名称" clearable size="small" style="width: 240px"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="角色标识" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入角色标识" clearable size="small" style="width: 240px"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="角色状态" clearable size="small" style="width: 240px">
          <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resourceList">
      <el-table-column label="资源编号" prop="id" width="75"/>
      <el-table-column label="资源类型" prop="resourceType" :show-overflow-tooltip="true" width="75"/>
      <el-table-column label="资源标题" prop="resourceTitle" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="子标题" prop="resourceSubtitle" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="资源链接" prop="resourceUrl" :show-overflow-tooltip="true" width="250"/>
      <el-table-column label="资源描述" prop="resourceDescription" :show-overflow-tooltip="true" width="300"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="资源类型" prop="name">
          <el-select v-model="form.resourceType" placeholder="请选择资源类型">
            <el-option
              v-for="type in resourceType"
              :key="type.code"
              :label="type.name"
              :value="type.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="资源标题" prop="name">
          <el-input v-model="form.resourceTitle" placeholder="请输入资源标题"/>
        </el-form-item>
        <el-form-item label="资源子标题" prop="code">
          <el-input v-model="form.resourceSubtitle" placeholder="请输入资源子标题"/>
        </el-form-item>
        <el-form-item label="资源链接" prop="code">
          <el-input v-model="form.resourceUrl" placeholder="请输入资源链接"/>
        </el-form-item>
        <el-form-item label="资源描述" prop="sort">
          <el-input type="textarea" placeholder="请输入详细描述信息" v-model="form.resourceDescription"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Resource",
  data() {
    return {
      // 请求地址
      baseUrl: "http://localhost:8080/nirvana-admin",
      // 资源类型，后续配置到数据库或者后端程序处理
      resourceType: [
        {
          code: 'toolkit',
          name: '技术工具'
        },
        {
          code: 'picture',
          name: '图片资源'
        },
        {
          code: 'audit',
          name: '音频资源'
        },
        {
          code: 'video',
          name: '视频资源'
        }
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示新增弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: undefined,
        code: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        label: "name",
        children: "children"
      },
      // 表单校验
      rules: {
        type: [
          {required: true, message: "类型不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        url: [
          {required: true, message: "url不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      axios.get(this.baseUrl + "/resources").then(
        response => {
          this.resourceList = response.data.data;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        resourceType: undefined,
        resourceTitle: undefined,
        resourceSubtitle: undefined,
        resourceUrl: undefined,
        resourceDescription: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      axios.get(this.baseUrl + "/resources/" + id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改资源";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id === undefined) {
            axios.post(this.baseUrl + "/resources", this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            axios.put(this.baseUrl + "/resources", this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除角色编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delRole(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }
  }
};
</script>
